BigInteger sumOfPowers(BigInteger n, BigInteger pow) {
    n.toString().toList()*.toBigInteger().collect { it**pow }.sum()
}


List find(limit, power) {
    result = []
    (2..limit).each { n ->
        if (n == sumOfPowers(n, power)) result << n
    }
    result
}

answer = find(1000000, 5)
println "${answer.sum()}: $answer"


